﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Inventory_BOMView : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    PagedDataSource pds = new PagedDataSource();

    public int CurrentPage
    {
        get
        {
            if (ViewState["CurrentPage"] == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt16(ViewState["CurrentPage"].ToString());
            }
        }
        set
        {
            ViewState["CurrentPage"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BindDataList();
            BindDDLSelectItem();
        }

       
    }

    //protected void BindDataList()
    //{
    //    string sql = "select IVR_BOM.BOMID,IVR_BOM.BOMREMARKS, IVR_BOM.EDITINTRAN, IVR_BOM.BOMACTIVE, IVR_ITEM.ITMDESC, IVR_UOM.UOMSYMBOL,          IVR_BOM.ITMQTY from IVR_BOM inner join  IVR_Item on (IVR_BOM.ITMID = IVR_ITEM.ITMID) inner join IVR_UOM on (IVR_Item.Primary_UOMID =        IVR_UOM.UOMID)";
    //    DataTable DT = DU.getdata(sql);
    //    pds.DataSource = DT.DefaultView;
    //    pds.AllowPaging = true;
    //    pds.PageSize = 5;
    //    pds.CurrentPageIndex = CurrentPage;
    //    LnkBtnNext.Enabled = !pds.IsLastPage;
    //    LnkBtnPrevious.Enabled = !pds.IsFirstPage;
    //    DataListBom.DataSource = pds;
    //    DataListBom.DataBind();
    //    DoPaging();
    //}
    //private void DoPaging()
    //{
    //    DataTable DT = new DataTable();
    //    DT.Columns.Add("PageIndex");
    //    DT.Columns.Add("PageText");
    //    for (int i = 0; i < pds.PageCount; i++)
    //    {
    //        DataRow DR = DT.NewRow();
    //        DR[0] = i;
    //        DR[1] = i + 1;
    //        DT.Rows.Add(DR);
    //    }
    //    DLPaging.DataSource = DT;
    //    DLPaging.DataBind();
    //}
    protected void DataListBom_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string BOMID = DataListBom.DataKeys[e.Item.ItemIndex].ToString();
        ImageButton ImgBtn1 = (ImageButton)e.Item.FindControl("ImgBtn");
        //ImgBtn1.Attributes.Add("onclick", "javascript:doPrint('divprint');");
        ImgBtn1.Attributes.Add("onclick", "PrintBOM('" + DDLSelectItem.SelectedValue + "','" + BOMID + "');");
        //string sql = "select IVR_BOMITEM.BOMID, IVR_BOMITEM.ITMQTY, IVR_ITEM.ITMDESC, IVR_UOM.UOMSYMBOL from IVR_BOMITEM inner join IVR_Item on (IVR_BOMITEM.ITMID = IVR_ITEM.ITMID) inner join IVR_UOM on (IVR_Item.Primary_UOMID = IVR_UOM.UOMID) and BOMID= " + key;
        //DataTable dt = DU.getdata(sql);
        SqlParameter[] prrbom = {
                                new SqlParameter("@BOMID",BOMID)
                              };
        DataTable dt = DU.getdatausingprocedure("usp_ivr_get_BomItem_Body", prrbom);
        if (dt.Rows.Count > 0)
        {
            GridView GrdBom = (GridView)e.Item.FindControl("GrdBom");
            GrdBom.DataSource = dt;
            GrdBom.DataBind();
        }
    }

   
    //protected void LnkBtnPrevious_Click(object sender, EventArgs e)
    //{
    //    CurrentPage -= 1;
    //    BindDataList();
    //}
    //protected void LnkBtnNext_Click(object sender, EventArgs e)
    //{
    //    CurrentPage += 1;
    //    BindDataList();
    //}
    //protected void DLPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    //{
    //    LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("LnkBtnPaging");
    //    if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
    //    {
    //        lnkbtnPage.Enabled = false;
    //        lnkbtnPage.Font.Bold = true;
    //    }
    //}
    //protected void DLPaging_ItemCommand(object source, DataListCommandEventArgs e)
    //{
    //    if (e.CommandName.Equals("Paging"))
    //    {
    //        CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
    //        BindDataList();
    //    }
    //}
   
    protected void DDLSelectItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sql = "select IVR_BOM.BOMID,IVR_BOM.BOMREMARKS, IVR_BOM.EDITINTRAN, IVR_BOM.BOMACTIVE, IVR_ITEM.ITMDESC, IVR_UOM.UOMSYMBOL,          IVR_BOM.ITMQTY from IVR_BOM inner join  IVR_Item on (IVR_BOM.ITMID = IVR_ITEM.ITMID) inner join IVR_UOM on (IVR_Item.Primary_UOMID =        IVR_UOM.UOMID) where IVR_BOM.ITMID =" + DDLSelectItem.SelectedValue;
        DataTable DT = DU.getdata(sql);
       
        //pds.DataSource = DT.DefaultView;
        //pds.AllowPaging = true;
        //pds.PageSize = 1;
        //pds.CurrentPageIndex = CurrentPage;
        //LnkBtnNext.Enabled = !pds.IsLastPage;
        //LnkBtnPrevious.Enabled = !pds.IsFirstPage;

        DataListBom.DataSource = DT;
        DataListBom.DataBind();
        
    }

    protected void BindDDLSelectItem()
    {
        string sql = "select ITMDESC, ITMID from IVR_ITEM where ITMID in (select Itmid from Ivr_bom  )order by ITMDESC";
        DataTable dt = DU.getdata(sql);

        DDLSelectItem.DataSource = dt;
        DDLSelectItem.DataTextField = "ITMDESC";
        DDLSelectItem.DataValueField = "ITMID";
        DDLSelectItem.DataBind();

        DDLSelectItem.Items.Insert(0, new ListItem("-Select Item-", "0"));
    }
    protected void DataListBom_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "EditBOM")
        {
            string key = e.CommandArgument.ToString();

            Response.Redirect("EditBOM.aspx?bomid="+key);
        }
    }
}
